package lb.io.utils;

import org.apache.commons.codec.binary.Base64;

import java.io.*;

public class SystemUtil {
    public static String hostname() throws RuntimeException {
        return Cmd.command("hostname");
    }

    public static String serial() throws RuntimeException {
        String shell = "prtconf | grep \"Machine Serial Number\"";
        String data = Cmd.command(shell);
        // 输出结果为: Machine Serial Number: 066A03P
        return data.split(":", 2)[1].trim();
    }

    public static long memory() throws RuntimeException {
        String shell = "prtconf -m | awk -F':' '{print $2}' | awk -F' ' '{print $1}'";
        String data = Cmd.command(shell);
        // 输出结果为: Memory Size: 15360 MB
        // 直接转化为 xx B
        return Long.parseLong(data) * 1024 * 1024;
    }

    public static String machine() {
        return "aix";
    }

    public static String processer() {
        return System.getProperty("os.arch");
    }

    // 清空文件内容
    public static void truncate(String filename) throws IOException {
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write("");
        fileWriter.flush();
        fileWriter.close();
    }

    public static void mkdir(String path) {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdir();
        }
    }

    public static void writeAll(String filename, String content) throws IOException {
        File file = new File(filename);
        if (!file.exists()) {
            file.createNewFile();
        }
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(content);
        fileWriter.flush();
        fileWriter.close();
    }

    public static String readAll(String filename) {
        String encoding = "UTF-8";
        File file = new File(filename);
        Long filelength = file.length();
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            return new String(filecontent, encoding);
        } catch (UnsupportedEncodingException e) {
            System.err.println("The OS does not support " + encoding);
            e.printStackTrace();
            return "";
        }
    }

    public static String base64Encode(String text) throws UnsupportedEncodingException {
        return new Base64().encodeToString(text.getBytes("UTF-8"));
    }

    public static String base64Decode(String text) throws UnsupportedEncodingException {
        return new String(new Base64().decode(text),"UTF-8");
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String s1 = base64Encode("hello world");

        String s2 = base64Decode(s1);

        System.out.println(s1);
        System.out.println(s2);
        assert s2.equals("hello world");
    }
}
